using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api.Mobile
{
    /// <summary>
    /// 隐患_隐患台账
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class YH_StandingBookMobileController : CoreApiController
    {
        private readonly YH_StandingBookService _service;
        /// <summary>
        /// YH_StandingBook
        ///</summary>
        public YH_StandingBookMobileController(YH_StandingBookService service)
        { 
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<YH_StandingBookDto> pagingQueryModel)
        {
            IPageModel<YH_StandingBookDto> result = await _service.GetList(pagingQueryModel);
            pagingQueryModel.Model.Type = "0";
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.Discoverer,//发现人
                a.DiscovererText,//发现人
                a.DiscoveryTime,//隐患发现时间
                a.RiskDescription,//隐患描述
                a.Schedule,//隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
                a.ScheduleText,//隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 非计划隐患上报列表
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetUnplannedList")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetUnplannedList(PagingModel<YH_StandingBookDto> pagingQueryModel)
        {
            pagingQueryModel.Model.Type = "1";
            IPageModel<YH_StandingBookDto> result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.Discoverer,//发现人
                a.DiscovererText,//发现人
                a.DiscoveryTime,//隐患发现时间
                a.RiskDescription,//隐患描述
                a.Schedule,//隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
                a.ScheduleText,//隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
                a.Status//状态(0保存/1提交)
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 隐患整改计划列表
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetPlanList")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.GetPlanList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetPlanList(PagingModel<YH_StandingBookDto> pagingQueryModel)
        {
            return await this.GetList(pagingQueryModel);
        }

        /// <summary>
        /// 隐患整改验收列表
        /// </summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetCheckList")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.GetCheckList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetCheckList(PagingModel<YH_StandingBookDto> pagingQueryModel)
        {
            return await this.GetList(pagingQueryModel);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(YH_StandingBookDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                //model.Type,//上报分类(计划/0非计划/外部机构)
                //model.ScreeningPlanProDetailId,//隐患计划排查明细ID
                //model.InstitutionTypes,//机构类别
                //model.CompanyName,//单位名称
                //model.Discoverer,//发现人
                //model.Monitor,//带班长
                //model.ApprovalOpinion,//带班长审批意见
                //model.ApprovalDate,//带班长审批时间
                model.RiskRegion,//隐患所属区域
                model.RiskPosition,//隐患位置
                model.OrganizationType,//组织类别
                model.InspectType,//检查类别
                model.DiscoveryTime,//隐患发现时间
                model.RiskType,//隐患分类
                model.RiskLevel,//隐患等级ID
                model.RiskDescription,//隐患描述
                //model.IsMajor,//是否为重大隐患
                //model.HaveContractor,//
                //model.Contractor,//
                //model.RectificationCase,//承包商名称ID
                //model.RectificationFund,//整改资金
                //model.Department,//整改责任部门
                //model.RectificationLeading,//整改责任人
                //model.Rectificationer,//整改人
                //model.PlanTime,//计划完成时间
                //model.HaveContingencyPlan,//是否制定应急预案
                //model.EmergencyPlan,//应急预案内容
                //model.CurrentPlan,//当前控制措施
                model.RectificationDescription,//整改描述
                //model.ActualTime,//实际完成时间
                //model.Acceptancer,//指定验收人
                //model.ApprovalResult,//验收意见
                //model.AppointDepartment,//验收部门
                //model.AppointPerson,//验收用户
                //model.AppointTime,//验收时间
                //model.Status,//状态(保存/提交)
                //model.Schedule,//隐患进度(/待审批/已生效/审批退回/验收通过/验收退回)
                //model.CreateFactory,//隶属工厂
                //model.CreateDept,//
                //model.UpdateDept,//
                model.YH_StandingBookAttachment,
                //model.YH_ScreeningDetail
            };
            return this.JsonSuccess(retObj);
        }


        /// <summary>
        ///  隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("UpdateaSchedule")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.UpdateaSchedule), "隐患进度")]
        public async Task<IActionResult> UpdateaSchedule(YH_StandingBookDto dto)
        {
            await _service.UpdateaSchedule(dto);
            return this.JsonSuccess();
        }



        /// <summary>
        ///  修改整改描述
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("UpdateaRectificationDescription")]
        [PermissionFilter(nameof(YH_StandingBookMobileController) + "_" + nameof(YH_StandingBookMobileController.UpdateaRectificationDescription), "整改描述")]
        public async Task<IActionResult> UpdateaRectificationDescription(YH_StandingBookDto dto)
        {
            await _service.UpdateaRectificationDescription(dto);
            return this.JsonSuccess();
        }

        #endregion


    }
}
